﻿using System;
using System.Windows.Forms;
using BLL_NhaSach;
using DAL_NhaSach;
using DevExpress.XtraEditors;

namespace AppQlyNhaSach.HoaDon
{
    public partial class frmPhieuThu : DevExpress.XtraEditors.XtraForm
    {
        PhieuThuTienBLL pttBLL;
        KhachHangBLL khBLL;
        public frmPhieuThu()
        {
            InitializeComponent();
        }

        private void frmPhieuThu_Load(object sender, EventArgs e)
        {
            pttBLL = new PhieuThuTienBLL(Properties.Settings.Default.ConnectionStringUser);
            grdPhieuThu.DataSource = pttBLL.getAllData();
            khBLL = new KhachHangBLL(Properties.Settings.Default.ConnectionStringUser);
           
            cboKH.DisplayMember = "TenKH";
            cboKH.ValueMember = "MaKH";
            cboKH.DataSource = khBLL.getDataCustom();
        }

        private void frmPhieuThu_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMainNS.isPhieuThu = false;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int ma = int.Parse(gridView1.GetFocusedRowCellValue("MaPhieuThu").ToString());
            PhieuThu pt =(PhieuThu)pttBLL.findObjects(ma);
            txtMa.Text = pt.MaPhieuThu.ToString();
            txtTen.Text = pt.KhachHang.TenKH;
            txtNgay.Text = pt.NgayThu.ToShortDateString();
            txtTien.Text = pt.TienThu.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

        }

        private void cboKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            DAL_NhaSach.KhachHang kh =(DAL_NhaSach.KhachHang)khBLL.findObjects(int.Parse(cboKH.SelectedValue.ToString()));
            txtCongNo.Text = kh.CongNo.ToString();

        }

        private void btnThem_Click_1(object sender, EventArgs e)
        {
            try
            {
                decimal congno = decimal.Parse(txtCongNo.Text);
                decimal tien = decimal.Parse(txtTienNhan.Text);
                if (tien <= congno)
                {
                    PhieuThu pt = new PhieuThu();
                    pt.MaKH = int.Parse(cboKH.SelectedValue.ToString());
                    pt.NgayThu = dtpNgay.Value;
                    pt.TienThu = tien;
                    if (pttBLL.addObject(pt))
                    {
                        XtraMessageBox.Show("Thêm phiếu thu thành công");
                        txtTienNhan.Text = "";
                        khBLL.truCongNo(pt.MaKH, pt.TienThu);
                        //cboKH.SelectedIndex = -1;
                        txtCongNo.Text = "";
                        grdPhieuThu.DataSource = pttBLL.getAllData();
                        grdPhieuThu.RefreshDataSource();
                    }
                    else XtraMessageBox.Show("Thêm phiếu thu thất bại");
                }
                else XtraMessageBox.Show("Số tiền phải trả không thể lớn hơn công nợ");

            }
            catch (Exception)
            {
                XtraMessageBox.Show("Tiền không hợp lệ");
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {

        }
    }
}